package com.jo.cch.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.TextView;

import com.github.promeg.pinyinhelper.Pinyin;
import com.jo.cch.R;
import com.jo.cch.adapter.LetterGridAdapter;
import com.jo.cch.adapter.PinyinAdapter;
import com.jo.cch.adapter.YingyuAdapter;
import com.jo.cch.bean.LetterInfo;
import com.jo.cch.helper.LetterDataHelper;
import com.jo.cch.helper.YingyuDataHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class YingyuActivity extends Activity {

    private AppCompatImageView backIv;

    private GridView gview;

    private LetterGridAdapter letterAdapter;

    private List<LetterInfo> datas = new ArrayList<>();

    private ExpandableListView expandableListView;

    final YingyuAdapter yyAdapter = new YingyuAdapter();

    private Button chimeStudy,startStudy, voiceStudy;

    private String selectGroup = "全部", selectChild = "全部";

    private TextView tips;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yingyu);
        tips = (TextView) findViewById(R.id.tips);
        gview = (GridView) findViewById(R.id.gview);
        datas = YingyuDataHelper.getAllDatas();
        //新建List
        letterAdapter = new LetterGridAdapter(this, datas);
        //配置适配器
        gview.setAdapter(letterAdapter);

        tips.setText("共"+datas.size()+"个音标和字母");

        expandableListView = (ExpandableListView) findViewById(R.id.expend_list);

        expandableListView.setAdapter(yyAdapter);

        YingyuDataHelper.letterChimeMap.forEach((k,v)->{
            System.out.println("letterWordMap.put(\""+k+"\",\""+ Pinyin.toPinyin(v, "").toLowerCase()+"\"");
        });

        //设置子项布局监听
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                v.setSelected(true);
                datas.clear();
                String group = yyAdapter.groupString[groupPosition];
                String child = yyAdapter.childString[groupPosition][childPosition];
                Map<String, List<LetterInfo>> groupData = YingyuDataHelper.datas.get(group);
                if(groupData != null){
                    selectGroup = group;
                    selectChild = child;
                    datas.addAll(YingyuDataHelper.getDatas(YingyuActivity.this, group, child));
                    tips.setText("共"+datas.size()+"个音标和字母");
                }
                letterAdapter.notifyDataSetChanged();
                return true;
            }
        });

        chimeStudy = (Button) findViewById(R.id.chimeStudy);
        chimeStudy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("yingyus", (Serializable) datas);
                if(selectGroup.equals("字母")){
                    bundle.putSerializable("yingyus", (Serializable) YingyuDataHelper.getZmDatas());
                }
                Intent intent = new Intent(YingyuActivity.this, YingyuchimeActivity.class);
                intent.putExtra("selectGroup", "英语");
                intent.putExtra("selectChild", selectGroup.equals(selectChild) ? selectGroup : selectGroup+"/"+selectChild);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        startStudy = (Button) findViewById(R.id.startStudy);
        startStudy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("yingyus", (Serializable) datas);
                if(selectGroup.equals("字母")){
                    bundle.putSerializable("yingyus", (Serializable) YingyuDataHelper.getZmDatas());
                }
                Intent intent = new Intent(YingyuActivity.this, YingyuquickActivity.class);
                intent.putExtra("selectGroup", "英语");
                intent.putExtra("selectChild", selectGroup.equals(selectChild) ? selectGroup : selectGroup+"/"+selectChild);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        voiceStudy = (Button) findViewById(R.id.voiceStudy);
        voiceStudy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("yingyus", (Serializable) datas);
                if(selectGroup.equals("字母")){
                    bundle.putSerializable("yingyus", (Serializable) YingyuDataHelper.getZmDatas());
                }
                Intent intent = new Intent(YingyuActivity.this, YingyuvoiceActivity.class);
                intent.putExtra("selectGroup", "英语");
                intent.putExtra("selectChild", selectGroup.equals(selectChild) ? selectGroup : selectGroup+"/"+selectChild);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


        //返回
        backIv = (AppCompatImageView) findViewById(R.id.backIv);
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
